package com.tech.cloudapp.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tech.cloudapp.biz.service.IBQuestionUserAnswerService;
import com.tech.cloudapp.db.entity.QuestionUserAll;
import com.tech.cloudapp.db.entity.QuestionUserAnswer;
import com.tech.cloudapp.db.service.QuestionUserAllService;
import com.tech.cloudapp.db.service.QuestionUserAnswerService;
import com.tech.cloudapp.exception.BusinessException;
import com.tech.cloudapp.model.CommonResponseDO;
import com.tech.cloudapp.po.QuestionUserAnswerParamPO;
import com.tech.cloudapp.util.BeanCopyUtils;
import com.tech.cloudapp.util.CopyUtils;
import com.tech.cloudapp.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
@Primary
@Service
public class BQuestionUserAnswerServiceImpl implements IBQuestionUserAnswerService {

    @Autowired
    private QuestionUserAnswerService questionUserAnswerServiceImpl;

    @Autowired
    private QuestionUserAllService questionUserAllServiceImpl;

    @Override
    public List<QuestionUserAnswer> list(QuestionUserAnswerParamPO questionUserAnswerParamPO) {
        QueryWrapper<QuestionUserAnswer> queryWrapper = new QueryWrapper();
        queryWrapper.eq("classify_id", questionUserAnswerParamPO.getClassifyId())
                .eq("paper_id", questionUserAnswerParamPO.getPaperId())
                .eq("user_id",questionUserAnswerParamPO.getUserId());
       return  this.questionUserAnswerServiceImpl.list(queryWrapper);
    }

    @Transactional
    @Override
    public void save(QuestionUserAnswerParamPO questionUserAnswerParamPO) throws BusinessException, IllegalAccessException, InstantiationException {
       //判断题目是否已经做过
        QueryWrapper<QuestionUserAnswer> queryWrapper = new QueryWrapper();
        queryWrapper.eq("classify_id", questionUserAnswerParamPO.getClassifyId())
                .eq("paper_id", questionUserAnswerParamPO.getPaperId())
                .eq("user_id",questionUserAnswerParamPO.getUserId());
        List<QuestionUserAnswer> list = this.questionUserAnswerServiceImpl.list(queryWrapper);
        //已经做过这套题
        if(list != null && list.size() > 0)
        {
          throw new BusinessException(CommonResponseDO.fail("-1","不能重复答题"));
        }

        List<QuestionUserAnswer> data = CopyUtils.copy(
                questionUserAnswerParamPO.getQuestionUserAnswers(),QuestionUserAnswer.class);

        //获取开始时间
      questionUserAnswerParamPO.getQuestionUserAnswers()
                .sort(new Comparator<QuestionUserAnswerParamPO.QuestionUserAnswerPO>() {
                    @Override
                    public int compare(QuestionUserAnswerParamPO.QuestionUserAnswerPO o1, QuestionUserAnswerParamPO.QuestionUserAnswerPO o2) {
                        return  (int)(Long.parseLong(o1.getAnswerTime()) - Long.parseLong(o2.getAnswerTime()));
                    }
                });

        String startTime = questionUserAnswerParamPO.getQuestionUserAnswers().get(0).getAnswerTime();
        int count = 0;
        for(QuestionUserAnswerParamPO.QuestionUserAnswerPO po : questionUserAnswerParamPO.getQuestionUserAnswers())
        {
            if(po.getIsRight() > 0)
            {
                count++;
            }
        }
        Double score = (double)100 * count / questionUserAnswerParamPO.getQuestionUserAnswers().size() ;
        // 数据入库
        this.questionUserAnswerServiceImpl.saveBatch(data);
        //生成考试成绩
        QuestionUserAll questionUserAll = new QuestionUserAll();
        questionUserAll.setUserId(questionUserAnswerParamPO.getUserId());
        questionUserAll.setClassifyId(questionUserAnswerParamPO.getClassifyId());
        questionUserAll.setPaperId(questionUserAnswerParamPO.getPaperId());
        questionUserAll.setStartTime(startTime);
        questionUserAll.setEndTime(String.valueOf(System.currentTimeMillis()));
        questionUserAll.setScore(Util.DoubleFormat(score,"#.#"));
        questionUserAll.setIsDone(true);

        this.questionUserAllServiceImpl.save(questionUserAll);
    }

    @Override
    public void replayAnswer(QuestionUserAnswerParamPO questionUserAnswerParamPO) {

        QueryWrapper<QuestionUserAnswer> queryWrapper = new QueryWrapper();
        queryWrapper.eq("classify_id", questionUserAnswerParamPO.getClassifyId())
                .eq("paper_id", questionUserAnswerParamPO.getPaperId())
                .eq("user_id",questionUserAnswerParamPO.getUserId());
        this.questionUserAnswerServiceImpl.replayAnswer(queryWrapper);
    }

    @Override
    public int wrongCollect(QuestionUserAnswerParamPO questionUserAnswerParamPO) {
        QueryWrapper<QuestionUserAnswer> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",questionUserAnswerParamPO.getUserId())
                .eq("is_right",0);
        int len = this.questionUserAnswerServiceImpl.list(queryWrapper).size();
        return len;
    }


}
